DAY5:Delete occurrences of an element if it occurs more than n times


Posted by birdbirdmurmur on 2023-07-18

題目連結:

Delete occurrences of an element if it occurs more than n times

解題思維:

  1. 要有一個儲存數字出現次數的「物件」
  2. 對arr遍歷篩選
  3. 給物件一個屬性
  4. 數字重複就+1 沒有就從1開始
  5. 直到n回傳

實作解法:

function deleteNth(arr,n){
  let count = {};
   return arr.filter( num =>{
     count[num] = (count[num] || 0) +1
     return count[num] <= n
})
}

心得

這題真的超難!
爬完discourse只稍微整理出一點邏輯
我想我目前的程度還不足以挑戰6kyu等級......
連續兩天在6kyu花了太多時間
接下來還是先回頭到7kyu好了


#javascript #Codewars #filter #object







Related Posts

SQL入門語法

SQL入門語法

CH4 字元與字串筆記

CH4 字元與字串筆記

[Python] How to setup a json or yaml to an object in python

[Python] How to setup a json or yaml to an object in python


Comments